package com.geektime.congestion.auth;

import com.geektime.congestion.auth.domain.ApiRequest;
import com.geektime.congestion.auth.domain.AuthToken;
import com.geektime.congestion.auth.service.CredentialStorage;
import com.geektime.congestion.auth.service.MemoryServiceImpl;
import com.google.common.collect.Maps;

import java.util.Map;

public class AuthMain {


    public static void main(String[] args) {
        CredentialStorage storage = new MemoryServiceImpl();
        long createTime = System.currentTimeMillis();
        String url = "/user";
        url += "?token=" + AuthToken.create(url, createTime, Maps.newHashMap(Map.of(
                "appId", "k1",
                "password", storage.getSecretFromDB("k1")
        ))).getToken();
        url += "&appId=k1&timestamp=" + createTime;
        System.out.println(url);

        ApiRequest apiRequest = ApiRequest.createFromFullUrl(url);
        System.out.println(apiRequest);

        AuthToken authToken = AuthToken.create(apiRequest.getBaseurl(), apiRequest.getTimestamp(), Maps.newHashMap(Map.of(
                "appId", apiRequest.getAppId(),
                "password", storage.getSecretFromDB(apiRequest.getAppId())
        )));
        System.out.println(authToken);

        if (authToken.isExpired()) {
            System.out.println("token expired");
            return;
        }
        if (!authToken.match(apiRequest.getToken())) {
            System.out.println("token not match");
            return;
        }
    }

}
